# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from xiangmu.items import LeisuItem

class LeisuSpider(CrawlSpider):
    name = 'leisu'
    allowed_domains = ['leisu.com']
    start_urls = ['https://data.leisu.com/zuqiu-388']

    rules = (
        Rule(LinkExtractor(allow=r'https://data.leisu.com/zuqiu-\d+'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        year = response.xpath('//span[@class="name"]/text()').extract_first().strip()
        tr_ele_list = response.xpath('//div[@id="matches"]/table/tr[position()>1]')
        source = response.xpath('//ul[@class="head-list"]/li[1]/text()').extract_first()
        for tr_ele in tr_ele_list:
            if len(tr_ele.xpath('./td')) == 8:
                round_num = tr_ele.xpath('./td[1]//text()').extract()
                time_list = tr_ele.xpath('./td[2]/span/text()').extract()
                host_team = tr_ele.xpath('./td[3]/a/text()').extract_first().strip()
                score = tr_ele.xpath('./td[4]/a/text()').extract_first().strip()
                client_team = tr_ele.xpath('./td[5]/a/text()').extract_first().strip()
                round_num = ''.join([item.strip() for item in round_num if item.strip() != ''])
                time_list = ' '.join([item.strip() for item in time_list if item.strip() != ''])
                print(source)
                print(round_num)
                print(time_list)
                print(host_team)
                print(score)
                print(client_team)
                print(year)
                print('--'*20)
        i = LeisuItem()
        i['source'] = source
        i['round_num'] = round_num
        i['time_list'] = time_list
        i['host_team'] = host_team
        i['score'] = score
        i['client_team'] = client_team
        i['year'] = year
        #i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract()
        #i['name'] = response.xpath('//div[@id="name"]').extract()
        #i['description'] = response.xpath('//div[@id="description"]').extract()
        return i
